<template>
  <el-drawer v-model="drawerVisible" :modal="false" :destroy-on-close="true" size="600px" title="新增客户">
    <el-form
      ref="ruleFormRef"
      label-position="top"
      require-asterisk-position="right"
      :inline="true"
      :rules="rules"
      :disabled="drawerProps.isView"
      :model="drawerProps.row"
      class="form-inline-wrap"
    >
      <el-form-item label="客户名称" prop="username">
        <el-input v-model="drawerProps.row!.username" placeholder="请输入客户名称" clearable></el-input>
      </el-form-item>
      <el-form-item label="联系方式" prop="contact">
        <el-input v-model="drawerProps.row!.contact" placeholder="请输入联系方式" clearable></el-input>
      </el-form-item>
      <el-form-item label="业务类型">
        <el-select v-model="drawerProps.row!.businessType" placeholder="请选择业务类型" clearable>
          <el-option v-for="item in businessType" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="客户来源">
        <el-input v-model="drawerProps.row!.source" placeholder="请输入客户来源" clearable></el-input>
      </el-form-item>
      <el-form-item label="客户行业">
        <el-input v-model="drawerProps.row!.industry" placeholder="请输入客户行业" clearable></el-input>
      </el-form-item>
      <el-form-item label="所在地">
        <el-input v-model="drawerProps.row!.location" placeholder="请输入所在地" clearable></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="drawerVisible = false">取消</el-button>
      <el-button v-show="!drawerProps.isView" type="primary" @click="handleSubmit">确定</el-button>
    </template>
  </el-drawer>
</template>

<script setup lang="ts" name="UserDrawer">
import { ref, reactive } from "vue";
import { ElMessage, FormInstance } from "element-plus";
import { User } from "@/api/interface";

const rules = reactive({
  username: [{ required: true, message: "请输入客户姓名" }],
  contact: [{ required: true, message: "请输入联系方式" }]
});

interface DrawerProps {
  title: string;
  isView: boolean;
  row: Partial<User.ResUserList>;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
}

const drawerVisible = ref(false);
const drawerProps = ref<DrawerProps>({
  isView: false,
  title: "",
  row: {}
});

// 业务类型
const businessType = ref([
  { label: "111", value: 1 },
  { label: "222", value: 2 }
]);

// 接收父组件传过来的参数
const acceptParams = (params: DrawerProps) => {
  drawerProps.value = params;
  drawerVisible.value = true;
};

// 提交数据（新增/编辑）
const ruleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  ruleFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      //   await drawerProps.value.api!(drawerProps.value.row);
      //   ElMessage.success({ message: `${drawerProps.value.title}用户成功！` });
      //   drawerProps.value.search!();
      //   drawerVisible.value = false;
    } catch (error) {
      console.log(error);
    }
  });
};

defineExpose({
  acceptParams
});
</script>
